{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "inception.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.8"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "n8CwINQcEBKz",
        "colab_type": "text"
      },
      "source": [
        "# Exporting ImageNet Inception\n",
        "\n",
        "_WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`_\n",
        "\n",
        "In this notebook, we'll show how to export the [pre-trained Imagenet Inception model](https://tfhub.dev/google/imagenet/inception_v3/classification/3) for serving."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3221z3P69fgf",
        "colab_type": "text"
      },
      "source": [
        "First, we'll install the required packages:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_SdQpq7g9LiI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install tensorflow==1.14.* tensorflow-hub==0.6.* boto3==1.*"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "I-k0gUpxDGkU",
        "colab_type": "text"
      },
      "source": [
        "Next, we'll download the model from TensorFlow Hub and export it for serving:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z6QLCzB4BKMe",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import time\n",
        "import tensorflow as tf\n",
        "import tensorflow_hub as hub\n",
        "from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def\n",
        "\n",
        "export_dir = \"export/\" + str(time.time()).split('.')[0]\n",
        "builder = tf.saved_model.builder.SavedModelBuilder(export_dir)\n",
        "\n",
        "with tf.Session(graph=tf.Graph()) as sess:\n",
        "    module = hub.Module(\"https://tfhub.dev/google/imagenet/inception_v3/classification/3\")\n",
        "\n",
        "    input_params = module.get_input_info_dict()\n",
        "    image_input = tf.placeholder(\n",
        "        name=\"images\", dtype=input_params[\"images\"].dtype, shape=input_params[\"images\"].get_shape()\n",
        "    )\n",
        "    \n",
        "    sess.run([tf.global_variables_initializer(), tf.tables_initializer()])\n",
        "\n",
        "    classes = module(image_input)\n",
        "    signature = predict_signature_def(inputs={\"images\": image_input}, outputs={\"classes\": classes})\n",
        "\n",
        "    builder.add_meta_graph_and_variables(\n",
        "        sess, [\"serve\"], signature_def_map={\"predict\": signature}, strip_default_attrs=True\n",
        "    )\n",
        "\n",
        "builder.save()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aGtJiyEnBgwl",
        "colab_type": "text"
      },
      "source": [
        "## Upload the model to AWS\n",
        "\n",
        "Cortex loads models from AWS, so we need to upload the exported model."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fTkjvSKBBmUB",
        "colab_type": "text"
      },
      "source": [
        "Set these variables to configure your AWS credentials and model upload path:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4xcDWxqCBPre",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "AWS_ACCESS_KEY_ID = \"\" #@param {type:\"string\"}\n",
        "AWS_SECRET_ACCESS_KEY = \"\" #@param {type:\"string\"}\n",
        "S3_UPLOAD_PATH = \"s3://my-bucket/image-classifier/inception\" #@param {type:\"string\"}\n",
        "\n",
        "import sys\n",
        "import re\n",
        "\n",
        "if AWS_ACCESS_KEY_ID == \"\":\n",
        "    print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_ACCESS_KEY_ID\"), file=sys.stderr)\n",
        "\n",
        "elif AWS_SECRET_ACCESS_KEY == \"\":\n",
        "    print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Please set AWS_SECRET_ACCESS_KEY\"), file=sys.stderr)\n",
        "\n",
        "else:\n",
        "    try:\n",
        "        bucket, key = re.match(\"s3://(.+?)/(.+)\", S3_UPLOAD_PATH).groups()\n",
        "    except:\n",
        "        print(\"\\033[91m{}\\033[00m\".format(\"ERROR: Invalid s3 path (should be of the form s3://my-bucket/path/to/file)\"), file=sys.stderr)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "czZkjb1IBr-f",
        "colab_type": "text"
      },
      "source": [
        "Upload the model to S3:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M0b0IbyaBsim",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import os\n",
        "import boto3\n",
        "\n",
        "s3 = boto3.client(\"s3\", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)\n",
        "\n",
        "for dirpath, _, filenames in os.walk(\"export\"):\n",
        "    for filename in filenames:\n",
        "        filepath = os.path.join(dirpath, filename)\n",
        "        filekey = os.path.join(key, filepath[len(\"export/\"):])\n",
        "        print(\"Uploading s3://{}/{}...\".format(bucket, filekey), end = '')\n",
        "        s3.upload_file(filepath, bucket, filekey)\n",
        "        print(\" ✓\")\n",
        "\n",
        "print(\"\\nUploaded model export directory to \" + S3_UPLOAD_PATH)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pZQWoeZbE7Wc",
        "colab_type": "text"
      },
      "source": [
        "<!-- CORTEX_VERSION_MINOR -->\n",
        "That's it! See the [example on GitHub](https://github.com/cortexlabs/cortex/tree/master/examples/tensorflow/image-classifier) for how to deploy the model as an API."
      ]
    }
  ]
}
